﻿using SuperWebSocket;
using System;
using System.Net;
using System.Net.Sockets;

namespace SuperWebSocketServerTest
{
    class Program
    {
        static bool tempSaveImage = false;

        static SuperWebSocketServer server = new SuperWebSocketServer();

        static IPAddress GetServerIpAddress()
        {
            string strHostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
            foreach (IPAddress ip in ipEntry.AddressList)
            {
                server.ConsoleWrite.Message("IPAddress", ip.ToString(),DateTime.Now, "SuperWebSocketServerTest", "");
                server.ConsoleWrite.Message("AddressFamily", ip.AddressFamily.ToString(), DateTime.Now, "SuperWebSocketServerTest", "");
                ////IP V4
                //if (ip.AddressFamily == AddressFamily.InterNetwork)
                //    return ip;
                //if (ip.AddressFamily == AddressFamily.Unix)
                //    return ip;
            }

            return ipEntry.AddressList[0];
        }

        static void Main(string[] args)
        {
            //var ip = GetServerIpAddress();

            //server.Configure.IPAddress = System.Net.IPAddress.Parse("192.168.23.128");
            //server.Configure.Port = 2345;

            //server.ConsoleWrite.WriteType = WebSocketConsoleWriteType.textfile;
            //server.ConsoleWrite.WriteLevel = ConsoleWriteLevel.release;

            server.OnStarted += new WebSocketEventHandler(OnServerStarted);
            server.OnCreated += new WebSocketEventHandler(OnWebSocketCreated);
            server.OnLogined += new WebSocketEventHandler(OnWebSocketLogined);
            server.OnMessage += new WebSocketEventHandler(OnWebSocketMessage);
            server.OnImage += new WebSocketEventHandler(OnWebSocketImage);
            server.OnLogout += new WebSocketEventHandler(OnWebSocketLogout); 

            server.Start();

            //Console.ReadKey();
        }

        static bool OnServerStarted(object sender,WebSocketEventArgs e)
        {
            Console.WriteLine(e.Message);
            return false;
        }

        static bool OnWebSocketCreated(object sender, WebSocketEventArgs e)
        {
            return false;
        }

        static bool OnWebSocketLogined(object sender, WebSocketEventArgs e)
        {
            WebSocketClient client = (WebSocketClient)sender;
            //server.SendData(client.Id, System.Text.Encoding.UTF8.GetBytes("王晓东 im 测试"));
            //server.SendMessage(client.Id, "王晓东 im 测试");
            return false;
        }

        static bool OnWebSocketMessage(object sender, WebSocketEventArgs e)
        {
            WebSocketContextData data = (WebSocketContextData)e.Data;
            Console.WriteLine(e.DateTime + " " + data.SendId + " 对 " + data.ReceiveId + " 说:" + data.Data);
            return false;
        }

        static bool OnWebSocketImage(object sender, WebSocketEventArgs e)
        {
            WebSocketContextData data = (WebSocketContextData)e.Data;
            WebSocketImageData wsImageData = (WebSocketImageData)data.Data;

            if (tempSaveImage)
            {
                string temp = AppDomain.CurrentDomain.BaseDirectory + "temp//";
                if (!new System.IO.FileInfo(temp).Exists)
                    System.IO.Directory.CreateDirectory(temp);

                using (System.IO.Stream stream = new System.IO.FileStream(temp + wsImageData.Name + "." + wsImageData.Format,System.IO.FileMode.Create,System.IO.FileAccess.Write))
                {
                    byte[] ImageData = (byte[])wsImageData.Data;
                    stream.Write(ImageData,0,ImageData.Length);
                }

            }

            Console.WriteLine(e.DateTime + " " + data.SendId + " 对 " + data.ReceiveId + " 传送图片:" + wsImageData.Name + "." + wsImageData.Format + " 大小:" + wsImageData.ImageSize);
            return false;
        }

        static bool OnWebSocketLogout(object sender, WebSocketEventArgs e)
        {           
            return false;
        }
               
    }
}
